package com.lgy.controller;

import com.lgy.pojo.Company;
import com.lgy.pojo.Employee;
import com.lgy.service.ICompanyService;
import com.lgy.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/company")
public class CompanyController {
    @Autowired
    private ICompanyService companyService;
    @RequestMapping("/register/isExist.do")
    @ResponseBody
    public ResultData companyIsExist(@RequestParam("companyname") String companyName){
        Boolean exist = companyService.findByCompanyName(companyName);
        if(exist){
            return ResultData.ok(true);
        }
        return ResultData.error("该公司名已存在,请重新输入");
    }
///company/insertCompany
    @RequestMapping("/register/insertCompany.do")
    @ResponseBody
    public ResultData<Employee> insertCompany(Company company){
        int insert = companyService.insertSelective(company);
        if(insert>0){//需要修改
            Employee employee = new Employee();
            employee.setCompanyid(company.getCompanyid());
            return ResultData.ok(employee);
        }
        else return ResultData.error("注册失败，请重新尝试");
    }

    ///login/isExistCompanyName
    @RequestMapping("/login/isExistCompanyName.do")
    @ResponseBody
    public ResultData isExistCompanyName(@RequestParam("companyname") String companyName){
        Boolean exist = companyService.findByCompanyName(companyName);
        if(!exist){
            return ResultData.ok(true);
        }else return ResultData.error("不存在该公司名称");
    }

    //company/getCompanyId.do
    @RequestMapping("/login/getCompanyId.do")
    @ResponseBody
    public ResultData getCompanyId(@RequestParam("companyname")String companyname){
        Company company = companyService.findCompanyName(companyname);
        if(company!=null){
            return ResultData.ok(company);
        }
        return ResultData.error("找不到该公司ID");
    }
}
